﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Purchase;
using Eb.MessageBox;
using UserManage;

public partial class Modules_Purchase_ucManuallySendForApproval : System.Web.UI.UserControl
{
    EB.Inbox.Inbox inBoxCreate = new EB.Inbox.Inbox();
    Purchase.Purchase purchase = new Purchase.Purchase();
    FrameWorkMain.FrameWorkMain frameWork = new FrameWorkMain.FrameWorkMain();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
           // GetUsers();
            GetUserGroups();
        }
        //if (lstUsers.SelectedIndex > 0)
        //{
        //    DataSet userDS = purchase.GetUsers(4);
        //    int index = lstUsers.SelectedIndex;
        //    lstUsers.ToolTip = userDS.Tables[0].Rows[index]["UserName"].ToString();
        //}
    }
    void GetUsers(int userGroupId)
    {

        //users coming under the selected group
        DataSet userDS = purchase.GetUsers(userGroupId);
        lstUsers.DataSource = userDS;
        lstUsers.DataTextField = "UserName";
        lstUsers.DataValueField = "UD_UserPersonalID";
        lstUsers.DataBind();
    } 


    void GetUserGroups()
    {
        DataSet usergroupDS = frameWork.GetUsergroup();
        ddlUserGroup.DataSource = usergroupDS;
        ddlUserGroup.DataValueField = "US_GroupID";
        ddlUserGroup.DataTextField = "US_GroupName";
        ddlUserGroup.DataBind();
        ddlUserGroup.Items.Insert(0, "-Select-");
    }
  

    protected void btnMove1_Click(object sender, EventArgs e)
    {
        if (lstUsers.SelectedIndex == -1)
        {
            MessageBox.Show("Select User");
            return;
        }
        foreach (int i in lstUsers.GetSelectedIndices())
        {

            lstSelectedUsers.Items.Add(new ListItem(lstUsers.SelectedItem.ToString(), lstUsers.SelectedValue));
            lstUsers.Items.RemoveAt(lstUsers.SelectedIndex);
            //LinkButton lk = new LinkButton();
            //lk.Text = "View";
            //lstUsers.Attributes.Add(lk);

        }
    }
    protected void btnMove2_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstUsers.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstSelectedUsers.Items.Count; j++)
                if (lstUsers.Items[i].Value == lstSelectedUsers.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstSelectedUsers.Items.Add(new ListItem(lstUsers.Items[i].Text, lstUsers.Items[i].Value));
            }
        }
        lstUsers.Items.Clear();
    }
    protected void btnMove3_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstSelectedUsers.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstUsers.Items.Count; j++)
                if (lstSelectedUsers.Items[i].Value == lstUsers.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstUsers.Items.Add(new ListItem(lstSelectedUsers.Items[i].Text, lstSelectedUsers.Items[i].Value));
            }
        }
        lstSelectedUsers.Items.Clear();
    }
    protected void btnMove4_Click(object sender, EventArgs e)
    {
        if (lstSelectedUsers.SelectedIndex == -1)
        {
            MessageBox.Show("Select User");
            return;
        }
        else
        {
            foreach (int i in lstSelectedUsers.GetSelectedIndices())
            {
                lstUsers.Items.Add(new ListItem(lstSelectedUsers.SelectedItem.ToString(), lstSelectedUsers.SelectedValue));
                lstSelectedUsers.Items.RemoveAt(lstSelectedUsers.SelectedIndex);
            }
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "Purchase/ucPurchaseOrderRequest.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        SendMultipleReQuest();

    }

   
   
    void SendMultipleReQuest() 
    {
      
        int userId = 0;
        if (Session["UserId"] != null)
            userId = Convert.ToInt16(Session["UserId"].ToString());
        int linkKey =Convert.ToInt32(Server.HtmlEncode(Request.QueryString["linkKey"]));
        int requestTypeId = Convert.ToInt32(Server.HtmlEncode(Request.QueryString["RequestType"]));
        int requestLinkUniqId = Convert.ToInt32(Server.HtmlEncode(Request.QueryString["RequestLinkId"])); 
        bool status;
        if (lstSelectedUsers.Items.Count == 0)
        {
            MessageBox.Show("There is no user in "+' '+ ddlUserGroup.SelectedItem.Text+' ' + "Group!!Plese select another Group");
            return;
        }
        if (lstSelectedUsers.Items.Count > 0)
        {
            for (int i = 0; i < lstSelectedUsers.Items.Count; i++)
            {
                // lstSelectedUsers.Items[i].Value);
                // status = purchase.InsertReQuestStatus(linkKey, requestTypeId, userId);

                //status = purchase.InsertReQuestStatus(linkKey, requestTypeId,Convert.ToInt32(lstSelectedUsers.Items[i].Value));
                int inBoxUniqId = inBoxCreate.CreateInBox(requestTypeId, Convert.ToInt32(lstSelectedUsers.Items[i].Value), linkKey);
                status = inBoxCreate.UpdateInBoxStatus(inBoxUniqId, userId, 1, "");
            }
            status = purchase.UpdateStepStatus(2, 3, requestLinkUniqId);
        }
        MessageBox.Show("Request Send for approval");
        btnSave.Enabled = false;

    }
    protected void lstUsers_PreRender(object sender, EventArgs e)
    {
        if (ddlUserGroup.SelectedIndex != 0)
        {
            int usergroupId = Convert.ToInt16(ddlUserGroup.SelectedValue);
            DataSet userDS = purchase.GetUsers(usergroupId);
            if (userDS.Tables[0].Rows.Count > 0)
            {
                foreach (ListItem item in lstUsers.Items)
                {
                    for (int j = 0; j < userDS.Tables[0].Rows.Count; j++)
                    {

                        if (userDS.Tables[0].Rows[j]["UserName"].ToString() == item.Text)
                        {
                            item.Attributes.Add("title", "User Id:" + userDS.Tables[0].Rows[j]["UD_UserPersonalID"].ToString() + " " + "UserName:" + userDS.Tables[0].Rows[j]["UserName"].ToString());
                            // item.Attributes.Add("title_style", "font-weight: bold;");

                            break;

                        }
                    }
                    //i++;
                }

            }
        }
    }
    protected void ddlUserGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlUserGroup.SelectedIndex != 0)
        {
            int usergroupId = Convert.ToInt16(ddlUserGroup.SelectedValue);
            if (usergroupId != 0)
                GetUsers(usergroupId);
        }
        else
        {
            lstUsers.Items.Clear();
            lstSelectedUsers.Items.Clear();
        }
    }
}
